// // import { Md5 } from 'ts-md5';

import { _decorator, log } from 'cc';
const { ccclass, property } = _decorator;

import HttpRequest from "../utils/HttpRequest";

@ccclass('WebApiMgr')
export default class WebApiMgr {
    //    // private static _ins: WebApiMgr;
    //    // static get Instance() { return this._ins };
    static Domain = 'https://bclll-api.kidikidi.net/';
    //    // static LoginApi = '/user/getAccountId';
    static LoginApi = '/userv/getAuntId';
    //    // 
    static ReadDataApi = '/user/getData';
    static SaveDataApi = '/user/saveData';
    static GetConfigApi = '/config/getConfig';
    static ConversionApi = '/juliang/conversion';
    static KSConversionApi = `/kuaishou/conversion`;
    static _accountId: string = 'hidfile_tt__000w8UGf_SEBjeyfSIWdTgLc-TFpoWtnLtd';

    //    // protected onLoad(): void {
    //    //     if (null != WebApiMgr._ins) {
    //    //         this.node.destroy();
    //    //     } else {
    //    //         WebApiMgr._ins = this;
    //    //         cc.game.addPersistRootNode(this.node);
    //    //     }
    //    // }

    static async login(data: { code?: string, appid?: string, platform: string, mac_id?: string }) {
        try {
            console.log("http post request login data-----", data);

            const json: string = await new Promise((res, rej) => HttpRequest.post(WebApiMgr.Domain + WebApiMgr.LoginApi, data, { "content-type": "application/json" }, false, res, rej));
            const res: { code: number, message: string, account_id: string, data: any } = JSON.parse(json);
            console.log("res", res);
            console.log("res.code", res.code);

            if (res.code == 200) {
                WebApiMgr._accountId = res.account_id;
                return res.account_id;
            } else {
                throw new Error(res.message);
            }
        } catch (err) {
            throw err;
        }
    }
    static async readData() {
        const res: string = await new Promise((res, rej) => {
            HttpRequest.post(WebApiMgr.Domain + WebApiMgr.ReadDataApi, { account_id: WebApiMgr._accountId }, { "content-type": "application/json" }, false, res, rej);
        });
        const data: { code: number, data: string } = JSON.parse(res);
        if (data.code == 200) {
            log(data.data);
            return data.data;
        } else {
            throw new Error('read data from server error');
        }
    }
    static async saveData(data: string) {
        //        // cc.log(data);
        //        // const sault = 'UD7uSMS6u0';
        //        // const check = Md5.hashStr(this._accountId + data + sault);
        //        // const res: string = await new Promise((res, rej) => {
        //        //     HttpRequest.post(WebApiMgr.Domain + WebApiMgr.SaveDataApi, { account_id: this._accountId, data, check }, { "content-type": "application/json" }, false, res, rej);
        //        // });
        //        // if (JSON.parse(res).code === 200) {
        //        //     //console.log(`保存数据成功`);
        //        // } else {
        //        //     //console.log(`保存数据失败`);
        //        // }
        //        // return JSON.parse(res).code == 200;
    }
    static async conversion(eventType: string, query?: object, other?: object) {
        const event_type = eventType == 'active' ? 'active' : 'game_addiction';
        let data: any = { query, userid: WebApiMgr._accountId, event_type, other }
        if (event_type == 'game_addiction') {
            data.original_events = eventType;
        }
        const res: string = await new Promise((res, rej) => {
            HttpRequest.post(WebApiMgr.Domain + WebApiMgr.ConversionApi, data, { "content-type": "application/json" }, false, res, rej);
        });
        return res;
    }
    static async KSConversion(eventType: string, query: any) {
        const event_type = eventType == 'active' ? 'active' : 'game_addiction';
        query.event_type = event_type;
        query.userid = WebApiMgr._accountId;
        const res: string = await new Promise((resolve, reject) => {
            HttpRequest.post(WebApiMgr.Domain + WebApiMgr.KSConversionApi, query, { "content-type": "application/json" }, false, resolve, reject);
        });
        return res;
    }
    static async getConfig(version: string) {
        const res: string = await new Promise((res, rej) => {
            HttpRequest.post(WebApiMgr.Domain + WebApiMgr.GetConfigApi, { version }, { "content-type": "application/json" }, false, res, rej);
        });
        const { code, data } = JSON.parse(res);
        if (code == 200) {
            return data;
        } else {
            throw new Error("get config from server error");
        }
    }
}
